package com.gec.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.system.SysCategory;
import com.gec.model.vo.SysCategoryQueryVo;
import com.gec.service.SysCategoryService;
import com.gec.system.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dafei
 * @since 2023-05-08
 */
@Api("影视分类")
@RestController
@RequestMapping("/admin/system/sysCategory")
public class SysCategoryController {
    @Autowired
    private SysCategoryService sysCategoryService;

    @ApiOperation("获取全部分类")
    @GetMapping("/findAllCategory")
    public Result findAllCategory(){
        List<SysCategory> list = this.sysCategoryService.list();
        return Result.ok(list);
    }

    @ApiOperation("根据id删除一个分类")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Long id){
        boolean byId = sysCategoryService.removeById(id);
        if(byId){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("添加影视分类")
    @PostMapping("/addCategory")
    public Result addCategory(@RequestBody SysCategory sysCategory){
        boolean save = this.sysCategoryService.save(sysCategory);
        if(save){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    @ApiOperation("查找单个影视")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Long id){
        SysCategory sysCategory = this.sysCategoryService.getById(id);
        return Result.ok(sysCategory);
    }
    @ApiOperation("修改影视分类")
    @PostMapping("/updateCategory")
    public Result updateCategory(@RequestBody SysCategory sysCategory){
        boolean update = this.sysCategoryService.updateById(sysCategory);
       if(update){
           return Result.ok();
       }else{
           return Result.fail();
       }
    }

    //todo 2023:050822:06 批量删除未完成
    @ApiOperation("批量删除")
    @DeleteMapping("/deleteIds")
    public Result deleteIds(@RequestBody List<Long> ids){
        System.out.println("ids = " + ids);
        boolean b = this.sysCategoryService.removeByIds(ids);
        if(b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //todo 2023:050822:06 模糊查询未完成
    @ApiOperation("分页查询")
    @GetMapping("/{page}/{limit}")
    public Result findCategoryByPageQuery(@PathVariable Long page, @PathVariable Long limit, SysCategoryQueryVo sysCategoryQueryVo) {
        System.out.println(sysCategoryQueryVo.getName());
        //1.创建分页对象
        IPage<SysCategory> p1 = new Page<>(page,limit);
        //2.调用方法
        p1 = this.sysCategoryService.selectPage(p1,sysCategoryQueryVo);
        //3.返回
        return Result.ok(p1);
    }
}

